/*****************************************************************************
 * opcodes.h: CPM opcode format definitions
 *****************************************************************************
 * Copyright (C) 2012
 *
 * Authors: Dan Larkin <danielhlarkin@gmail.com>
 *          Yonatan Naamad <cached@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *****************************************************************************/

#ifndef CPM_OPCODES
#define CPM_OPCODES

#include "datatypes.h"
#include <stdint.h>

#define CPM_CODE_JOIN       0
#define CPM_CODE_ALLOCATE   1
#define CPM_CODE_REQUEST    2
#define CPM_CODE_FREE       3
#define CPM_CODE_LIST       4
#define CPM_CODE_EXPOSIT    5
#define CPM_CODE_GET        6
#define CPM_CODE_GIVE       7

struct cpm_op_join
{
    uint8_t code;
    uint8_t key[16];
    int32_t bw_total;
    int32_t disk_total;
    int16_t slots_total;
    int16_t slots_used;
    cpm_torrent *torrent_list;
} __attribute__ ((packed));

struct cpm_op_allocate
{
    uint8_t code;
    int32_t bw_min;
    int32_t bw_max;
    uint8_t torrent_id[20];
} __attribute__ ((packed));

struct cpm_op_request
{
    uint8_t code;
    int32_t bw;
    uint8_t torrent_id[20];
} __attribute__ ((packed));

struct cpm_op_free
{
    uint8_t code;
    int32_t bw;
    uint8_t torrent_id[20];
} __attribute__ ((packed));

struct cpm_op_list
{
    uint8_t code;
    uint8_t key[16];
} __attribute__ ((packed));

struct cpm_op_exposit
{
    uint8_t code;
    int32_t len;
    cpm_torrent *torrent_list;
} __attribute__ ((packed));

struct cpm_op_get
{
    uint8_t code;
    uint8_t torrent_id[20];
} __attribute__ ((packed));

struct cpm_op_give
{
    uint8_t code;
    int32_t len;
    uint8_t *file;
} __attribute__ ((packed));

typedef struct cpm_op_join cpm_op_join;
typedef struct cpm_op_allocate cpm_op_allocate;
typedef struct cpm_op_request cpm_op_request;
typedef struct cpm_op_free cpm_op_free;
typedef struct cpm_op_list cpm_op_list;
typedef struct cpm_op_exposit cpm_op_exposit;
typedef struct cpm_op_get cpm_op_get;
typedef struct cpm_op_give cpm_op_give;

#endif
